/*
 * Written by Dustin Striplin
 */

package View;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import Actions.AuthorViewReviews;
import Actions.SubmitManuscript;
import Actions.UnsubmitPaper;
import Actions.UpdatePaper;
import Actions.ViewManuscriptAction;
import FileParser.FileParser;
import Model.Author;
import Model.Manuscript;
import Model.Roles;
import Model.User;
import TableModels.AuthorTemplate;



public class AuthorView extends JFrame {
	

	private static final long serialVersionUID = 1L;  //to get rid of warning
	public static final int WIDTH = 850;
	public static final int HEIGHT = 600;
	public static final int ROW_HEIGHT = 30;
	
	private static final int BUTTON_WIDTH = 100;
	private static final int BUTTON_HEIGHT = 50;
	private JFrame mainFrame;
	private Author author;
	JTable table;	//table that is displayed on main screen
	
	/**
	 * Creates an author view
	 * @param a_author The user that is logged in
	 */
	public AuthorView (Author a_author) {
		author = a_author;
		mainFrame = new JFrame();
	}
	
	/**
	 * Display the gui
	 */
	public void start() {

		mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		mainFrame.setSize(WIDTH, HEIGHT);
		mainFrame.setTitle("Author View");
		mainFrame.setResizable(false);
		mainFrame.setLocationRelativeTo(null);		//set in the middle of the screen
		mainFrame.setLayout(new BorderLayout());
		mainFrame.add(createTopPanel(author.getName(), author.getConference().getName(), Roles.AUTHOR.toString()), BorderLayout.NORTH);  //use the enum Roles for the role string
		mainFrame.add(createMidPanel(), BorderLayout.CENTER);
		mainFrame.add(createBotPanel(), BorderLayout.SOUTH);
		mainFrame.setVisible(true);
	}
	
	/**
	 * Creates the top panel that contains name, conference, & role info.
	 */
	private JPanel createTopPanel(final String name, final String conference, final String role) {
		JPanel topPnl = new JPanel();
		topPnl.setBackground(Color.LIGHT_GRAY);	
		topPnl.setPreferredSize(new Dimension(WIDTH, 2*HEIGHT/16));	//set this panel to 2/16 of the JFrame
		topPnl.setLayout(new GridLayout(3,1,5,5)); //3 rows, 1 columns, hgap = 5, vgap = 5
		
		

		JLabel nameLbl 		= new JLabel("Logged in as: " + name, JLabel.LEFT);      //create labels for user info 
		JLabel conferenceLbl= new JLabel("Conference:   " + conference, JLabel.LEFT); //that is to be displayed
		JLabel roleLbl		= new JLabel("Role:                " + role, JLabel.LEFT);
		topPnl.add(nameLbl);
		topPnl.add(conferenceLbl);
		topPnl.add(roleLbl);
		
		add(topPnl);
		return topPnl;
	}
	
	/**
	 * Creates the middle panel that contains the table
	 */
	private JPanel createMidPanel() {
		JPanel midPnl = new JPanel();
		midPnl.setBackground(Color.WHITE);	
		Dimension prefSize = new Dimension(WIDTH, 11*HEIGHT/16);
		midPnl.setPreferredSize(prefSize);	//set this panel to 11/16 of the JFrame
		midPnl.setLayout(new FlowLayout());  //FlowLayout, so the contents will not take up all
											//available space.
		
		midPnl.add(createTable(author.getManuscripts(), prefSize));
		add(midPnl);
		return midPnl;
	}
	
	/**
	 * Creates the bottom panel that contians teh buttons
	 */
	private JPanel createBotPanel() {
		JPanel botPnl = new JPanel();
		botPnl.setBackground(Color.DARK_GRAY);
		botPnl.setPreferredSize(new Dimension(WIDTH, 3*HEIGHT/16));  //set this panel to 3/16 of the JFrame
		FlowLayout layout = new FlowLayout(FlowLayout.CENTER);
		layout.setVgap(30);										//vertical gap for the buttons
		layout.setHgap(25);										//horizontal gap for the buttons
		botPnl.setLayout(layout);
		
		JButton viewM 	= new JButton(new ViewManuscriptAction(this.table, author));
		JButton viewR = new JButton(new AuthorViewReviews(this.table, author));				//to use, insert your Action, follow
		JButton submit = new JButton(new SubmitManuscript(this.table, author));				//ViewManuscriptAction as a template
		JButton update = new JButton(new UpdatePaper(this.table, author));	
		JButton unsubmit = new JButton(new UnsubmitPaper(this.table,author));	
		JButton signOut = new JButton("Sign out");
		signOut.addActionListener(new ActionListener()
	      {
	        public void actionPerformed(final ActionEvent the_event){
	        	mainFrame.dispose();
	        	FileParser fp = new FileParser();
	        	User user = new User(fp);
	    		UserView userView = new UserView(user, fp);	//need file parser to generate lists of conferences
	    		userView.start();
	        }
	      });
		Dimension buttonDim = new Dimension(BUTTON_WIDTH,BUTTON_HEIGHT);		//size of a button
		viewM.setPreferredSize(buttonDim);					
		viewR.setPreferredSize(buttonDim);
		submit.setPreferredSize(buttonDim);
		update.setPreferredSize(buttonDim);
		unsubmit.setPreferredSize(buttonDim);
		signOut.setPreferredSize(buttonDim);
		
		
		botPnl.add(viewM);
		botPnl.add(viewR);
		botPnl.add(submit);
		botPnl.add(update);
		botPnl.add(unsubmit);
		botPnl.add(signOut);
		
		add(botPnl);
		return botPnl;
	}
	
	/**
	 * Sets up the table
	 * @param scripts The manuscripts
	 * @param prefSize the preferred size
	 * @return A JScrollPane that contains the table
	 */
	private JScrollPane createTable(ArrayList<Manuscript> scripts, Dimension prefSize) {

		table = new JTable(new AuthorTemplate(scripts));
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setPreferredSize(new Dimension((int)prefSize.getWidth() - 24,(int)prefSize.getHeight() - 20));
		table.setFillsViewportHeight(true);  //true, so some of the cells may be off screen.
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  //only a single row can be selected
		table.setColumnSelectionAllowed(false);
		table.setAutoCreateRowSorter(true);				//allows sorting by clicking a column
		
		table.setRowHeight(ROW_HEIGHT);					//big enough to hold 4 reviews
															
		return(scrollPane);
	}


}